class Solution(object):
    def calcEquation(self, equations, values, queries):
        """
        :type equations: List[List[str]]
        :type values: List[float]
        :type queries: List[List[str]]
        :rtype: List[float]
        """
        c = {}
        for i, equation in enumerate(equations):
            if equation[0] not in c:
                if equation[1] not in c:
                    c[equation[1]] = (equation[1], 1.0)
                    c[equation[0]] = (equation[1], values[i])
                else:
                    value = values[i]
                    e = equation[1]
                    while c[e][0] != e:
                        value *= c[e][1]
                        e = c[e][0]
                    c[equation[0]] = (e, value)
            else:
                if equation[1] not in c:
                    value = 1 / values[i]
                    e = equation[0]
                    while c[e][0] != e:
                        value *= c[e][1]
                        e = c[e][0]
                    c[equation[1]] = (e, value)
                else:
                    value_0 = values[i]
                    e1 = equation[0]
                    while c[e1][0] != e1:
                        value_0 /= c[e1][1]
                        e1 = c[e1][0]
                    value_1 = 1
                    e2 = equation[1]
                    while c[e2][0] != e2:
                        value_1 *= c[e2][1]
                        e2 = c[e2][0]
                    c[e1] = (e2, value_0 * value_1)
        out = [0.0] * len(queries)
        print(c)
        for i, query in enumerate(queries):
            if query[0] not in c or query[1] not in c:
                out[i] = -1.0
            else:
                value_0 = 1
                e1 = query[0]
                while c[e1][0] != e1:
                    value_0 *= c[e1][1]
                    e1 = c[e1][0]
                value_1 = 1
                e2 = query[1]
                while c[e2][0] != e2:
                    value_1 *= c[e2][1]
                    e2 = c[e2][0]
                if e1 == e2:
                    out[i] = round(value_0 / value_1, 5)
                else:
                    out[i] = -1.0
        return out


data = Solution()
# equations = [["a", "b"], ["b", "c"], ["bc", "cd"]]
# values = [1.5, 2.5, 5.0]
# queries = [["a", "c"], ["c", "b"], ["bc", "cd"], ["cd", "bc"]]
# print(data.calcEquation(equations, values, queries))
# equations = [["a", "b"], ["b", "c"]]
# values = [2.0, 3.0]
# queries = [["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"]]
# print(data.calcEquation(equations, values, queries))
# equations = [["a", "b"]]
# values = [0.5]
# queries = [["a", "b"], ["b", "a"], ["a", "c"], ["x", "y"]]
# print(data.calcEquation(equations, values, queries))
# equations = [["a", "b"], ["e", "f"], ["b", "e"]]
# values = [3.4, 1.4, 2.3]
# queries = [["b", "a"], ["a", "f"], ["f", "f"], ["e", "e"], ["c", "c"], ["a", "c"], ["f", "e"]]
# print(data.calcEquation(equations, values, queries))
# equations = [["a", "b"], ["c", "d"]]
# values = [1.0, 1.0]
# queries = [["a","c"],["b","d"],["b","a"],["d","c"]]
# print(data.calcEquation(equations, values, queries))
equations = [["a","b"],["c","b"],["d","b"],["w","x"],["y","x"],["z","x"],["w","d"]]
values = [2.0,3.0,4.0,5.0,6.0,7.0,8.0]
queries = [["a","c"],["b","c"],["a","e"],["a","a"],["x","x"],["a","z"]]
print(data.calcEquation(equations, values, queries))


